﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;
using System.IO;
using System.Web.UI.HtmlControls;

namespace WebSite.admin
{
    public partial class banner : BaseAdmin
    {

        public HtmlInputFile File8;
        public HtmlForm form1;


        // Methods
        private void AddPicture(string imgpath, string imgnote, string albumid,string classId)
        {
            SQLAccess.DoSql("insert into DB_Flash (flashpic,flashurl,visible,modelid,classId) values ('" + imgpath + "','" + imgnote + "',1," + albumid + "," + this.ddlClass.SelectedItem.Value + ")");
        }

        private void bind()
        {
            //string sql = "select * from DB_Model";
            //DataSet set = SQLAccess.GreatDs(sql);
            //this.DataList1.DataSource = set;
            //this.DataList1.DataBind();
            //string str2 = "select top 1 * from DB_Model where visible= 1";
            //DataTable table = new DataTable();
            //table = SQLAccess.GreatDs(str2).Tables[0];
            //if (table.Rows.Count > 0)
            //{
                //this.Label1.Text = "宽 \x00d7 高：" + table.Rows[0]["width1"].ToString() + " \x00d7 " + table.Rows[0]["height1"].ToString();
                DataSet set2 = SQLAccess.GreatDs("select * from DB_Flash  order by flashid asc");
                this.DataList2.DataSource = set2;
                this.DataList2.DataBind();
          //  }
            if ((base.Request.QueryString["flashid"] != null) && (base.Request.QueryString["action"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    SQLAccess.DoSql("delete from DB_Flash where flashid=" + base.Request.QueryString["flashid"].ToString() + "");
                }
                base.Response.Write("<script language=javascript>alert('删除成功');window.location.href='banner.aspx';</script>");
                base.Response.End();
            }
        }

        public void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string albumid = info.model("");
            foreach (Control control in this.DataList1.Controls)
            {
                RadioButton button = (RadioButton)control.FindControl("rbttruck");
                if (button.Checked)
                {
                    albumid = button.GroupName;
                }
            }
            HttpFileCollection files = HttpContext.Current.Request.Files;
            StringBuilder builder = new StringBuilder("");
            string[] strArray = base.Request.Form["text"].Split(new char[] { ',' });
            for (int i = 0; i < files.Count; i++)
            {
                if (files[i].FileName.Length > 0)
                {
                    HttpPostedFile file = files[i];
                    if ((file.ContentLength / 0x400) > 0x400)
                    {
                        builder.Append(Path.GetFileName(file.FileName) + "---不能大于1024k<br>");
                        break;
                    }
                    string extension = Path.GetExtension(file.FileName);
                    if (((extension != ".jpg") && (extension != ".JPG")) && ((extension != ".gif") && (extension != ".GIF")))
                    {
                        builder.Append(Path.GetFileName(file.FileName) + "---图片格式不对，只能是jpg或gif<br>");
                        break;
                    }
                }
            }
            if (builder.Length <= 0)
            {
                string str3 = "pic001";
                string path = base.Server.MapPath("../uploadfile/flash") + @"\" + str3;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                Random random = new Random();
                int num2 = 1;
                this.upcontent(albumid);
                for (int j = 0; j < files.Count; j++)
                {
                    HttpPostedFile file2 = files[j];
                    string fileName = "";
                    string str6 = "";
                    string imgpath = "";
                    fileName = Path.GetFileName(file2.FileName);
                    string str8 = random.Next(100, 0x5f5e100).ToString() + num2.ToString();
                    string str9 = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + str8;
                    if (fileName.Length > 0)
                    {
                        str6 = Path.GetExtension(file2.FileName);
                        string filename = path + @"\" + str9 + str6;
                        file2.SaveAs(filename);
                        imgpath = @"flash\" + str3 + @"\" + str9 + str6;
                    }
                    if (str6.ToString() != "")
                    {
                        this.AddPicture(imgpath, strArray[j], albumid,this.ddlClass.SelectedItem.Value);
                    }
                    num2++;
                }
                base.Response.Write("<script language='javascript'>alert('Flash播放器管理更新成功');window.location.href='banner.aspx';</script>");
                base.Response.End();
            }
        }

        public void Page_Load(object sender, EventArgs e)
        {
          
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.FlashAdvertConfiguration);
                this.BindClass();
              this.bind();
              
            }
        }

        public void SetFalseone(object sender, EventArgs e)
        {
            RadioButton button = (RadioButton)sender;
            DataListItem parent = (DataListItem)button.Parent;
            for (int i = 0; i < this.DataList1.Items.Count; i++)
            {
                if (i != parent.ItemIndex)
                {
                    RadioButton button2 = (RadioButton)this.DataList1.Items[i].FindControl("rbttruck");
                    button2.Checked = false;
                }
                else
                {
                    RadioButton button3 = (RadioButton)this.DataList1.Items[i].FindControl("rbttruck");
                    string sql = "select top 1 * from DB_Model where modelid=" + button3.GroupName + "";
                    DataTable table = new DataTable();
                    table = SQLAccess.GreatDs(sql).Tables[0];
                    this.Label1.Text = "宽 \x00d7 高：" + table.Rows[0]["width1"].ToString() + " \x00d7 " + table.Rows[0]["height1"].ToString();
                    DataSet set = SQLAccess.GreatDs("select * from DB_Flash where  modelid=" + table.Rows[0]["modelid"].ToString() + " order by flashid asc");
                    this.DataList2.DataSource = set;
                    this.DataList2.DataBind();
                }
            }
        }

        private void upcontent(string albumid)
        {
            string sql = "select * from DB_Flash where modelid=" + albumid + " order by flashid asc";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    SQLAccess.DoSql("update [DB_Flash] set [flashurl]='" + base.Request.Form["content_" + row["flashid"].ToString()] + "' where flashid=" + row["flashid"].ToString() + "");
                }
            }
        }

        private void BindClass()
        {
            string sql = "select classId,ClassName from DB_Class ";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.ddlClass.DataTextField = "className";
            this.ddlClass.DataValueField = "classId";
            this.ddlClass.DataSource = table;
            this.ddlClass.DataBind();
        }

        protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bind();
        }


    }
}